package phr_interface;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Interface {
	private SSLConnection ssl;
	private Identity identity = null;
	private BufferedReader stdin = null;
	private boolean isWork = true; 
	private HealthRecord hr = null;


	public Interface(String ip, int port) {
		ssl = new SSLConnection(ip, port);
		InputStream inputstream = System.in;
		InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
		stdin = new BufferedReader(inputstreamreader);
	}

	public void run()
	{
		System.out.println("\n-Health Information Service Provider-");
		System.out.println(" [please log in]");
		logIn();
		loadHealthRecord(identity.id);
		hr.printHealthRecord();
		try {
			ssl.writeLine(new Message().setMessage("logoff").toXml());
			ssl.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public boolean logIn() {
		String id;
		String pass;

		try {
			ssl.connect();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("check your internet connection or PHR agent is currently unavailable");
			return false;
		}

		try {
			System.out.print("Id: ");
			id = stdin.readLine();

			if (System.console() == null) {
				System.out.print("Password: ");
				pass = stdin.readLine();
			}
			else {
				pass = new String(System.console().readPassword("Password: "));
			}			
			
			ssl.writeLine(new Message().setMessage("logIn")
									.setIdentity(id, pass, null, null).toXml());
			
			Message message = new Message(ssl.readLine());

			if (message.getMessage().equals("authorized")) {
				System.out.println("logged in");
				identity = new Identity(message.getIdentityAttribute("xml"));
			}
			else {
				ssl.close();
			}
			//TODO Login
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	public void loadHealthRecord(String requestId) {
		System.out.println("loading health report for " + requestId);
		try {
			ssl.writeLine(new Message().setMessage("requestHealthRecord")
					.setIdentity(null, null, null, identity.getXMLString())
					.setHealthRecord(requestId, null, null, null, null, null)
					.toXml());
			Message message = new Message(ssl.readLine());

			System.out.println("loading 1 = " + message.toXml());
			if (message.getMessage().equals("accepted")) {
				message = new Message(ssl.readLine());
				System.out.println("loading 2 = " + message.toXml());
				if (message.getMessage().equals("authorized"))
					hr = new HealthRecord(requestId, message.getHealthRecordAttribute("identifier"), message.getHealthRecordAttribute("private"), message.getHealthRecordAttribute("public"));
			}
			else if (message.getMessage().equals("denied")){
				System.out.println("you have no access.");
			}
			else {
				System.out.println("there is no record.");
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
